//#include "threadpool.hpp"
#include "threadpool-final.hpp"

// class SumTask : public Task
// {
// public:
//     SumTask(long long time):time_(time)    //指明获取要结果
//     {

//     }
//     virtual Any run()
//     {
        
//         //std::this_thread::sleep_for(std::chrono::seconds(2));
//         long long sum = 0;
//         for(int i = 0; i < time_; i++)
//         {
//             sum += i; 
//         }
        
//         return sum;
//     }

// private:
//     long long time_;
// };

// class SleepTask : public Task
// {
// public:
//     virtual Any run()
//     {
//         std::this_thread::sleep_for(std::chrono::seconds(5));
//         return 0;
//     }
// };

//用户提交的任务，所以认为用户知道结果的类型

int sumTask(int a, int b)
{
    int sum = 0;
    for(int i = a; i < b; i++)
    {
        sum += i;
    }
    std::this_thread::sleep_for(std::chrono::seconds(5));
    return sum;  
}

int main()
{
    {
        ThreadPool pool;
        pool.setMode(ThreadPoolMode::CACHED);
        pool.start(2);

        std::future<int> r1 = pool.submitTask([](int a, int b){
            int sum = 0;
            for(int i = a; i < b; i++)
            {
                sum += i;
            }
            //std::this_thread::sleep_for(std::chrono::seconds(5));
            return sum;
        },1,100);

        std::future<int> r2 = pool.submitTask([](int a, int b){
            int sum = 0;
            for(int i = a; i < b; i++)
            {
                sum += i;
            }
            //std::this_thread::sleep_for(std::chrono::seconds(5));
            return sum;
        },1,10000);

        std::future<int> r3 = pool.submitTask([](int a, int b){
            int sum = 0;
            for(int i = a; i < b; i++)
            {
                sum += i;
            }
            return sum;
        },1,10000);

        std::future<int> r4 = pool.submitTask([](int a, int b){
            int sum = 0;
            for(int i = a; i < b; i++)
            {
                sum += i;
            }
            return sum;
        },1,10000);

        cout << r1.get() << endl;
        cout << r2.get() << endl;
        cout << r3.get() << endl;
        cout << r4.get() << endl;
        cout << sizeof(std::function<int(int,int)>) << endl;
        // Result r1 = pool.submitTask(std::make_shared<SumTask>(100));
        // Result r2 = pool.submitTask(std::make_shared<SumTask>(20000));
        // Result r3 = pool.submitTask(std::make_shared<SleepTask>());
        // Result r4 = pool.submitTask(std::make_shared<SumTask>(30000));
        // Result r5 = pool.submitTask(std::make_shared<SumTask>(30000));
        // Result r6 = pool.submitTask(std::make_shared<SumTask>(30000));
        // Result r7 = pool.submitTask(std::make_shared<SumTask>(30000));        
        // Result r8 = pool.submitTask(std::make_shared<SumTask>(30000));        
     
        // cout << "task1 : " << *r1.getVal<long long>() << endl;
        // cout << "task2 : " << *r2.getVal<long long>() << endl;
        // cout << "task3 : " << *r3.getVal<int>() << endl;
        
        // cout << "task4 : " << *r4.getVal<long long>() << endl;
        // cout << "task5 : " << *r5.getVal<long long>() << endl;
        // cout << "task6 : " << *r6.getVal<long long>() << endl;
        // cout << "task7 : " << *r7.getVal<long long>() << endl;
    }


    //getchar();
    //pool.close();
    getchar();
    return 0;
}